﻿//创建人：longb
//日期：2022-07-25
//描述：工装工具配送查询
var page = {
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSSendRequestHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        page.addEditCell();

    },

    //页面事件绑定
    bind: function () {
        $('#OrderStatus').combobox({
            data: [{ id: 0, text: '待接收' }, { id: 5, text: '已接收' }, { id: 10, text: '配送中' }, { id: 15, text: '配送完' }, { id: 20, text: '收料完成' }, { id: 999, text: '全部'}],
            valueField: 'id',
            textField: 'text',
            onSelect: function (record) {
            }
        });
        $('#OrderStatus').combobox('setValue', 999);
        $('#OrderTabel').datagrid({
            //            checkOnSelect: false,
            //            selectOnCheck: false,
            //            singleSelect: true,
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
		         { field: 'TOGETHERFLAG', title: '任务单号', width: 180 },
                 { field: 'PRODUCTNAME', title: '件号', width: 120 },
                 { field: 'DESCRIPTION', title: '件号名称', width: 90 },
                 { field: 'WORKFLOWSTEPNAME', title: '阶段', width: 100 },
                 { field: 'WFDESC', title: '工艺名称', width: 120 },
                 { field: 'STEPNAME', title: '工序名称', width: 160 },
            //                 { field: 'PLANNEDSTARTDATE', title: '计划开始时间', width: 130 },
            //                 { field: 'PLANNEDFINISHDATE', title: '计划完成时间', width: 130 },
            //                 { field: 'APPLYTYPE', title: '配送提请类型', width: 100, align: 'center' },
            //                 { field: 'STATUS', title: '任务状态', width: 100, align: 'center' }
                ]],
            onSelect: function (index, row) {
                page.GetToolsData();
            },
            onCheckAll: function (rows) {
                page.GetToolsData();
            },
            onCheck: function (rowIndex, rowData) {
                page.GetToolsData();
            },
            onUncheck: function (rowIndex, rowData) {
                page.GetToolsData();
            }
        });
        $('#ToolsTabel').datagrid({
            columns: [[
		        { field: 'DRAWINGNO', title: '工装工具图号', width: 100 },
		        { field: 'TOOLNAME', title: '工装工具名称', width: 100 },
                { field: 'SPECIFICATION', title: '型号规格', width: 100 },
                { field: 'TOOLTYPE', title: '工装工具类型', width: 100 },
		        { field: 'QTY', title: '需求数量', width: 100, align: 'center' },
                { field: 'PRODUCTBATCHNO', title: '批次号', width: 100, align: 'center' },
                { field: 'APPLYQTY', title: '已准备数量', width: 100, align: 'center' },
                { field: 'APPLYQTY', title: '未准备数量', width: 100, align: 'center' },
                { field: 'PRODUCTDESC', title: '出库时间', width: 100 },
                { field: 'FULLNAME', title: '申请人', width: 100 },
                { field: 'APPLYTYPE', title: '使用状态', width: 100 }
                ]]
        });
    },
    //查询
    searchData: function () {
        clearMessage();
        $('#ToolsTabel').datagrid('loadData', []);
        page.GetZsRequiredTools();
    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
        clearMessage();
    },
    //查询任务单信息
    GetZsRequiredTools: function () {
        $('#ToolsTabel').datagrid('loadData', [])
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetZsRequiredToolsAll",
            TransportType: "1",
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            $('#OrderTabel').datagrid('loadData', res.DBTable);
        });
    },
    //获取工装工具提请信息
    GetToolsData: function () {
        var rows = $('#OrderTabel').datagrid('getChecked');
        var wfs = [];
        var ContainerNos = [];
        $.each(rows, function (index, item) {
            wfs.push(item.WORKFLOWSTEPID);
            ContainerNos.push(item.TOGETHERFLAG);
        });
        var param = {
            action: "GetToolsData",
            TransportType: "1",
            workflowstepid: wfs.join(','),
            ContainerNo: ContainerNos.join(',')
        };
        page.post(param, function (res) {
            $('#ToolsTabel').datagrid('loadData', res.DBTable);
        });
    },
    //接收配送提请任务
    ReciveToolsSend: function () {
        var rows = $('#OrderTabel').datagrid('getChecked');
        if (rows.length == 0) {
            displayMessage("请勾选任务单信息，再进行操作");
            return;
        }
        var param = {
            action: "ReciveToolsSend",
            TransportType: "1",
            data: JSON.stringify(rows)
        };
        page.post(param, function (res) {
            if (res.Result) {
                page.GetZsRequiredTools();
            }
            displayMessage(res.Message, res.Result);
        });

    },
    //开始配送
    StartToolsSend: function () {
        var row = $('#OrderTabel').datagrid('getSelected');
        var tools = $('#ToolsTabel').datagrid('getRows');
        var data = [];
        $.each(tools, function (index, item) {
            if (item.EDITORQTY > 0) {
                data.push(item);
            }
        });
        if (data.length == 0) {
            displayMessage("订单所需工装工具已配送完");
            return;
        }
        var param = {
            action: "StartToolsSend",
            workflowstepid: row.WORKFLOWSTEPID,
            EnginepartplanID: row.ENGINEPARTPLANID,
            APPLYTYPE: row.APPLYTYPE == "临时" ? "2" : "1",
            TransportType: "1",
            data: JSON.stringify(data)
        };
        page.post(param, function (res) {
            if (res.Result) {
                //                if (res.Message == "配送完成") {
                page.GetZsRequiredTools();
                $('#ToolsTabel').datagrid('loadData', []);
                //                } else {
                //                    page.GetToolsData();
                //                }
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },

    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});